package com.m.service.impl;

import com.m.dao.entity.BlackWhiteManagerEntity;
import com.m.dao.mapper.BlackWhiteManagerMapper;
import com.m.enums.BlackWhiteType;
import com.m.exception.RiskControllerException;
import com.m.pojo.dto.blackwhite.AddBlackWhiteInfoDto;
import com.m.pojo.dto.blackwhite.BlackWhiteInfoDto;
import com.m.pojo.dto.blackwhite.BlackWhiteSearchDto;
import com.m.service.IBlackWhiteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 功能：<br/>
 *
 * @author miracle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class BlackWhiteServiceImpl implements IBlackWhiteService {

    private static final Logger logger = LoggerFactory.getLogger(SceneServiceImpl.class);

    @Resource
    private BlackWhiteManagerMapper blackWhiteManagerMapper;

    @Override
    public void addBlackWhiteInfo(AddBlackWhiteInfoDto addBlackWhiteInfoDto) {
        if (addBlackWhiteInfoDto.getType() == null || addBlackWhiteInfoDto.getDimId() == null
                || addBlackWhiteInfoDto.getDimVal() == null || "".equals(addBlackWhiteInfoDto.getDimVal())){
            throw new RuntimeException("参数异常");
        }
        BlackWhiteManagerEntity blackWhiteManagerEntity = new BlackWhiteManagerEntity();
        blackWhiteManagerEntity.setType(addBlackWhiteInfoDto.getType());
        blackWhiteManagerEntity.setDimId(addBlackWhiteInfoDto.getDimId());
        blackWhiteManagerEntity.setDimVal(addBlackWhiteInfoDto.getDimVal());
        blackWhiteManagerMapper.insertUseGeneratedKeys(blackWhiteManagerEntity);
    }

    @Override
    public void delBlackWhiteInfo(Long blackeWhiteId) {
        // 校验ID是否存在
        BlackWhiteManagerEntity blackWhiteManagerEntity = blackWhiteManagerMapper.selectByPrimaryKey(blackeWhiteId);
        if (blackWhiteManagerEntity == null) {
            throw new RiskControllerException("id对应的信息不存在");
        }

        blackWhiteManagerMapper.deleteByPrimaryKey(blackeWhiteId);
    }

    @Override
    public List<BlackWhiteInfoDto> getBlackWhiteInfo(BlackWhiteSearchDto blackWhiteSearchDto) {
        List<BlackWhiteInfoDto> allBlackWhiteListInfos = blackWhiteManagerMapper.getAllBlackWhiteListInfos(blackWhiteSearchDto.getSceneName(),
                blackWhiteSearchDto.getEngName(), blackWhiteSearchDto.getType());
        Map<Integer, String> allBlackWhiteTypeMaps = BlackWhiteType.getAllBlackWhiteTypeMaps();
        if (allBlackWhiteListInfos != null) {
            allBlackWhiteListInfos.forEach(blackWhiteInfoDto -> {
                blackWhiteInfoDto.setBlackWhiteTypeName(allBlackWhiteTypeMaps.get(blackWhiteInfoDto.getType()));
            });
        }
        return allBlackWhiteListInfos;
    }
}
